package edu.wright.ceg4980.year2013.keyboard;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

@SuppressWarnings("serial")
public class PracticeScreen extends JPanel {
	private int numTries;
	private String pswd;
	
	private JEditorPane infoEditorPane;
	
	private JPasswordField pswdField;
	
	public PracticeScreen() {
		super(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
		numTries = 0;
		pswd = "";
		
		infoEditorPane = new JEditorPane("text/html", "");
		infoEditorPane.setEditable(false);
		infoEditorPane.setOpaque(false);
		infoEditorPane.setMaximumSize(new Dimension(500, Short.MAX_VALUE));
		updateInfoTA();
		
		pswdField = new JPasswordField(15);
		
		JPanel northPanel = new JPanel(new BorderLayout(0, 5));
		
		JPanel wrapper = new JPanel(new FlowLayout(FlowLayout.LEFT));
		
		add(northPanel, BorderLayout.NORTH);
			northPanel.add(infoEditorPane, BorderLayout.CENTER);
			northPanel.add(wrapper, BorderLayout.SOUTH);
				wrapper.add(pswdField);
	}
	
	public void setTries(int numTries) {
		this.numTries = numTries;
		updateInfoTA();
	}
	
	public void setCorrectPassword(String password) {
		this.pswd = password;
		updateInfoTA();
	}
	
	public char[] getEnteredPassword() {
		return pswdField.getPassword();
	}
	
	public void clearPassword() {
		pswdField.setText("");
	}
	
	private void updateInfoTA() {
		String infoString = String.format(
				"In order to ensure that your typing patterns are consistent, we need you to practice typing your password. " +
				"You need to type it correctly <strong>%d</strong> more times.<br/><br/>" +
				"As a reminder, your password is <strong>%s</strong>.",
				numTries,
				pswd);
		infoEditorPane.setText(infoString);
	}
}
